<template>
  <Preview
    v-show="isShowPreview"
    :name="formState.name"
    :number="formState.number"
    :profile="posterUrl"
    @back="isShowPreview = false"
    ref="preview"
  />
  <template v-if="!isShowPreview">
    <div class="home">
      <div class="container">
        <div class="input-row">
          <label for="name">名字</label>
          <div class="input-content">
            <input
              type="text"
              id="name"
              v-model="formState.name"
              maxlength="4"
              placeholder="请输入名字"
              @blur="handleBlur('name')"
            />
            <span v-if="isShowErrorMessage.name">名字不能为空</span>
          </div>
        </div>
        <div class="input-row" style="margin-bottom: 48px">
          <label for="number">客户数量</label>
          <div class="input-content">
            <input
              type="tel"
              id="number"
              v-model="formState.number"
              maxlength="3"
              placeholder="请输入客户数量"
              @blur="handleBlur('number')"
            />
            <span v-if="isShowErrorMessage.number">数量不能为空</span>
          </div>
        </div>
        <van-uploader :after-read="afterRead" :disabled="disabled">
          <van-button class="container-btn">上传照片生成海报</van-button>
        </van-uploader>
      </div>
    </div>
    <Cropper ref="cropper" @save="onSave" />
  </template>
</template>

<script setup lang="ts">
import Cropper from '@/components/Cropper.vue'
import Preview from '@/components/Preview.vue'
import { computed, reactive, ref } from 'vue'

const formState = reactive({
  name: '',
  number: undefined
})
const isShowErrorMessage = reactive({
  name: false,
  number: false
})

const isShowPreview = ref(false)
const posterUrl = ref()
const preview = ref<InstanceType<typeof Preview> | null>(null)
const cropper = ref<InstanceType<typeof Cropper> | null>(null)

function afterRead(file: object) {
  cropper.value?.open((file as { content: string })?.content)
}

function handleBlur(key: keyof typeof isShowErrorMessage) {
  isShowErrorMessage[key] = !formState[key]
}

/** Cropper组件处理完图片保存后触发 */
function onSave(url: string) {
  posterUrl.value = url
  isShowPreview.value = true
  preview.value?.createPoster()
}

const disabled = computed(() => !formState.name || !formState.number)
</script>

<style scoped lang="less">
.home {
  width: 100%;
  height: 100vh;
  background: url(@/assets/images/home-bg.png) no-repeat;
  background-size: 100% 100%;
  .container {
    width: 100%;
    padding: 0 40px;
    box-sizing: border-box;
    padding-top: 226px;
    position: relative;
    z-index: 1;
  }

  .input-row {
    margin-bottom: 24px;
  }

  .input-row label {
    display: block;
    margin-bottom: 12px;
    color: #ffa63e;
    font-size: 12px;
  }

  .input-row input {
    display: block;
    width: 100%;
    height: 50px;
    background: #f5f5f5;
    border-radius: 10px;
    border: 0;
    color: #333;
    padding: 0 17px;
    box-sizing: border-box;
    font-weight: 700;
  }

  .input-content {
    position: relative;
  }

  .input-content span {
    position: absolute;
    color: #ff0000;
    font-size: 12px;
    right: 14px;
    top: 0;
    z-index: 1;
    top: 50%;
    transform: translateY(-50%);
  }

  .container-btn {
    width: 298px;
    height: 50px;
    font-size: 16px;
    font-weight: 600;
    background: #ffa63e;
    border-radius: 100px;
    color: #840100;
    border-color: #ffa63e;
  }
}
</style>
